package com.atl.service.impl;

import com.atl.enity.po.Advertisement;
import com.atl.enity.web.dto.AdvertisementListDTO;
import com.atl.mapper.AdvertisementMapper;
import com.atl.service.IAdvertisementService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 
 * @since 2024-06-26
 */
@Service
public class AdvertisementServiceImpl extends ServiceImpl<AdvertisementMapper, Advertisement> implements IAdvertisementService {


    /**
     *
     * 根据条件查询
     *
     * @param advertisementListDTO
     * @return
     */
    @Override
    public Page<Advertisement> getAll(AdvertisementListDTO advertisementListDTO) {
        QueryWrapper<Advertisement> queryWrapper = new QueryWrapper<>();

        String adName = advertisementListDTO.getAdName();
        String adType = advertisementListDTO.getAdType();
        String status = advertisementListDTO.getStatus();
        LocalDateTime displayPeriodStart = advertisementListDTO.getDisplayPeriodStart();
        LocalDateTime displayPeriodEnd = advertisementListDTO.getDisplayPeriodEnd();

        if (StringUtils.isNotBlank(adName)) {
            queryWrapper.like("ad_name", adName);
        }

        if (StringUtils.isNotBlank(adType)) {
            queryWrapper.eq("ad_type", adType);
        }

        if (StringUtils.isNotBlank(status)) {
            queryWrapper.eq("status", status);
        }

        if (displayPeriodStart != null && displayPeriodEnd != null) {
            queryWrapper.between("display_period_start", displayPeriodStart, displayPeriodEnd);
        }

        return this.page(new Page<>(advertisementListDTO.getCurrent(), advertisementListDTO.getPageSize()), queryWrapper);
    }
}
